from rest_framework.views import APIView
from utils.result import R
from utils.aliverify_code import * 
from api.models import ItEduReplyComment,ItEduCommentLesson

class UserReplyCommentAPIView(APIView):

    """
    获取用户自己的评论（前5条）
    """
    def get(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")

        comment_list = list(ItEduReplyComment.objects.filter(user___id=user_pk).order_by("-_id")[:5].values())

        return R().data_result(comment_list = comment_list)


class ReplyCommentAPIView(APIView):

    """
    获取某一个评论的回复
    """
    def get(self,request,*args, **kwargs):

        comment_id = int(request.GET.get("comment_id"))
        offset = int(request.GET.get("offset"))

        reply_comment_list = list(ItEduReplyComment.objects.filter(comment_lesson___id=comment_id).order_by("-_id")[offset:offset+5].values(
            "user__avatar_image",
            "user__nickname",
            "content",
            "createtime",
            "_id"
        ))

        return R().data_result(reply_comment_list = reply_comment_list)

    """
    用户对评论发起回复
    """
    def post(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")
        comment_id = request.data.get("comment_id")
        content = request.data.get("content")

        comment = ItEduReplyComment.objects.create(
            user___id = user_pk,
            content = content,
            comment_lesson___id = comment_id
        )

        return R().data_result(comment_id = comment._id)
        
        
   
